import { request, useRequest } from 'umi';
import { getBaseUrl } from '@/services/urlconst';
import { getTenant, requestFunComponFiex } from '@/utils';

let baseUrl = getBaseUrl();
// let baseUrl = 'http://192.168.3.21:9006'; //小田本地

/* 新增布局信息 */
export const addLayout = async (params: any) => {
  let url = `${baseUrl}/commonController/addLayout`;
  try {
    return request(url, {
      method: 'POST',
      toObj: false,
      headers: {
        tenantName: getTenant(),
      },
      data: {
        ...params,
      },
      requestType: 'json',
      charset: 'utf8',
      skipErrorHandler: true,
    });
  } catch (e) {}
};

/* 编辑布局信息 */
export const editLayout = async (params: any) => {
  let url = `${baseUrl}/commonController/editLayout`;
  try {
    return request(url, {
      method: 'PUT',
      toObj: false,
      headers: {
        tenantName: getTenant(),
      },
      data: {
        ...params,
      },
      requestType: 'json',
      charset: 'utf8',
      skipErrorHandler: true,
    });
  } catch (e) {}
};

/**
 * 获取开启应用
 * @param getConfigByTypeCallback  开启应用回调
 *
 */
const getConfigByTypeService = (
  getConfigByTypeCallback: (res: any) => void,
) => {
  const getConfigByType = useRequest(
    (payload: any) => {
      let personUrl = `${baseUrl}/smsConfigController/getConfigByType`;
      return requestFunComponFiex('POST', personUrl, payload);
    },
    {
      manual: true,
      onSuccess: getConfigByTypeCallback,
    },
  );

  return {
    getConfigByTypeRun: getConfigByType?.run,
    getConfigByTypeLoading: getConfigByType?.loading,
  };
};

/**
 * 获取初始化数据
 * @param homePageSmsCallback
 *
 */
const homePageSmsService = (homePageSmsCallback: (res: any) => void) => {
  const homePageSms = useRequest(
    (payload: any) => {
      let personUrl = `${baseUrl}/smsCensusController/homePageStatistic`;
      return requestFunComponFiex('POST', personUrl, payload);
    },
    {
      manual: true,
      onSuccess: homePageSmsCallback,
    },
  );

  return {
    homePageSmsRun: homePageSms?.run,
    homePageSmsLoading: homePageSms?.loading,
  };
};

/**
 * 获取初始化数据  资产统计
 * @param homePageAssetCallback
 *
 */
const homePageAssetService = (homePageAssetCallback: (res: any) => void) => {
  const homePageAsset = useRequest(
    (payload: any) => {
      let personUrl = `${baseUrl}/assetInfoController/statistics`;
      return requestFunComponFiex('POST', personUrl, payload);
    },
    {
      manual: true,
      onSuccess: homePageAssetCallback,
    },
  );

  return {
    homePageAssetRun: homePageAsset?.run,
    homePageAssetLoading: homePageAsset?.loading,
  };
};

export { getConfigByTypeService, homePageSmsService, homePageAssetService };
